package networkSimulation.simulation.virus.statistic

import networkSimulation.simulation.virus.InfectablePerson
import networkSimulation.framework.{Graph, Edge}
import networkSimulation.statistic.{Statistic, VisualStatistics}

/**
 * Statistic representing the amount of infected people as a bar chart.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
class AmountInfectedBar[N<%InfectablePerson,E<%Edge[N]] extends Statistic[N,E]{
  private var infected = 0
  private var notInfected = 0
  private var infectedAmounts =List[Int]()
  private var notInfectedAmounts =List[Int]()

  /*
  *  Generate the visual statistic
  */
  @Override
  override def generateStatistic(vis:VisualStatistics,path : String){
        var a = List[Int]()
        var b = List[String]()
        a::= infectedAmounts.head
        b::= "Infected"
        a::= notInfectedAmounts.head
        b::= "Not Infected"
        vis.createBarChart("Infected ratio","Infected & Not Infected",a,b,"Nodes",path)
  }

  /*
  *  Take snapshots from the graph
  */
  @Override
  override def updateStatistics(graph: Graph[N, E],k:String){
    infected = 0
    notInfected = 0
    for(node: N <- graph.nodes){
      if(node.infected ==true){
        infected+=1
      }else{
        notInfected+=1
      }
    }
    key = k
  }

  /*
  *  Load the xml data into the statistic
  */
  @Override
  override def loadData(data : xml.NodeSeq){
      infectedAmounts ::= (data \\ "Infected").text.toInt
      notInfectedAmounts ::= (data \\ "NotInfected").text.toInt
  }

  /*
  *  Generates xml for the statistic
  */
  @Override
  override def toXml(): xml.NodeSeq  = {
    <Stat key={key}>
      <Infected>{ infected }</Infected>
      <NotInfected>{ notInfected }</NotInfected>
    </Stat>
  }
}